package com.aramis.mydytt.ui.search

import androidx.lifecycle.MutableLiveData
import com.aramis.mydytt.ui.base.BaseViewModel
import com.aramis.mydytt.ui.bean.MovieField
import com.aramis.mydytt.ui.bean.MovieItemBean
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class MainSearchViewModel : BaseViewModel() {

    val searchResultData = MutableLiveData<List<MovieItemBean>?>()

    fun getSearchResult(cate: MovieField, area: MovieField, year: MovieField, type: MovieField, sort: MovieField) {
        getSearchResult(cate.fieldId, area.fieldId, year.fieldId, type.fieldId, sort.fieldValueStr ?: MovieField.SORT_HOT)
    }

    fun getSearchResultDefault() {
        getSearchResult(0, 0, 0, 0, MovieField.SORT_HOT)
    }

    private fun getSearchResult(cateId: Int, areaId: Int, year: Int, typeId: Int, sort: String) {
        compositeDisposable.add(
            movieApi.getSearchResult(cateId, areaId, year, typeId, sort, 24, 1)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe({
                    searchResultData.postValue(it?.data)
                }, {
                    searchResultData.postValue(null)
                })
        )
    }
}